package part5assistclass;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch用法
 * 0 start
 * 2 start
 * 1 start
 * 4 start
 * 3 start
 * 5 start
 * main end
 */
public class CountDownLatchTest02 {


    public static void main(String[] args) throws InterruptedException {

        // 计数器
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " start");
                countDownLatch.countDown(); // 计数器-1
            }, String.valueOf(i)).start();
        }

        // 阻塞等待计数器归零
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + " end");

    }

}
